package com.tedu.springboot2302.controller;

import com.tedu.springboot2302.entity.Zzz_User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class Zzz_UserController {
    private static File zzz_Users;
    static {
        zzz_Users = new File("./zzz_users");
        if(!zzz_Users.exists()) {
            zzz_Users.mkdirs();
        }
    }

    @RequestMapping("/zzz_reg")
    public void zzz_reg(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if(username==null || username.isEmpty() ||
           password==null || password.isEmpty()
          )
        {
            try {
                response.sendRedirect("/zzz_reg_info_error.html");
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        File file = new File(zzz_Users,username+".obj");
        if(file.exists()) {
            try {
                response.sendRedirect("/zzz_reg_have_user.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        Zzz_User user = new Zzz_User(username,password);
        try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))
            )
        {
            oos.writeObject(user);
            response.sendRedirect("/zzz_reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/zzz_login")
    public void zzz_login(HttpServletRequest request,HttpServletResponse response) {
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            if(username==null || password==null) {
                    response.sendRedirect("/reg_info_error.html");
                return;
            }

            File file = new File(zzz_Users,username+".obj");

            if(file.exists()) {
                    ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
                    Zzz_User user = (Zzz_User)ois.readObject();
                    ois.close();
                    if(user.getPassword().equals(password)) {
                        response.sendRedirect("/zzz_login_success.html");
                    } else {
                        response.sendRedirect("/zzz_login_fail.html");
                    }
            } else {
                    response.sendRedirect("/zzz_login_fail.html");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
